package com.woniuxy.java106crm.controller;

import com.woniuxy.java106crm.annotation.RequireMethod;
import com.woniuxy.java106crm.entity.ServiceInvoice;
import com.woniuxy.java106crm.request.ResPage;
import com.woniuxy.java106crm.request.ResponseResult;
import com.woniuxy.java106crm.service.ServiceInvoiceService;
import com.woniuxy.java106crm.util.RandomUtils;
import com.woniuxy.java106crm.util.Status;
import com.woniuxy.java106crm.vo.SearchServiceVo;
import com.woniuxy.java106crm.vo.ServiceInvoiceVo;
import com.woniuxy.java106crm.vo.ServiceTypeCountVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 服务单据访问控制层
 */
@Api(tags = "服务管理模块API文档")
@Slf4j
@RestController
@RequestMapping("/service")
public class ServiceInvoiceController {

    @Resource
    private ServiceInvoiceService serviceInvoiceService;

    /**
     * 创建服务单据
     * @param serviceInvoice
     * @param session
     * @return
     */
    @RequireMethod("根据id修改计划")
    @PostMapping("/add")
    public ResponseResult<String>  addServiceInvoice(@RequestBody ServiceInvoice serviceInvoice, HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        serviceInvoice.setCreator(uid);
        serviceInvoice.setStatus(Status.SERVICE_STATUS_CREATE);
        serviceInvoice.setId(RandomUtils.createRandomStringUseTime("SE"));
        serviceInvoiceService.addServiceInvoice(serviceInvoice);
        return new ResponseResult<>(ResponseResult.OK,null);
    }

    /**
     * 服务分配界面多条件查询所有单据
     * @param searchServiceVo
     * @return
     */
    @RequireMethod("服务分配界面多条件查询所有单据")
    @PostMapping("/queryServiceInvoice")
    public ResponseResult<ResPage<List<ServiceInvoiceVo>>>  queryServiceInvoice(@RequestBody SearchServiceVo searchServiceVo){
//        log.debug("serviceInvoice:{}",serviceInvoiceVo);
        return new ResponseResult<>(ResponseResult.OK,serviceInvoiceService.queryServiceInvoice(searchServiceVo));
    }

    /**
     * 主管分配服务单据，修改对应单据的状态
     * @param id
     * @return
     */
    @RequireMethod("分配服务单据，修改对应单据的状态")
    @PutMapping ("/updateAssignStatus/{id}/{assigned}")
    public ResponseResult<String>  updateAssignStatus(@PathVariable("id")String id,@PathVariable("assigned")Integer assigned){
        serviceInvoiceService.updateAssignStatus(id,assigned);
        return new ResponseResult<>(ResponseResult.OK,null);
    }

    /**
     * 服务处理界面根据客户经理id查询状态为已分配的服务单据
     * @param searchServiceVo
     * @return
     */
    @RequireMethod("根据客户经理id查询状态为已分配的服务单据")
    @PostMapping ("/queryServiceInvoiceById")
    public ResponseResult<ResPage<List<ServiceInvoiceVo>>>  queryServiceInvoiceById(@RequestBody SearchServiceVo searchServiceVo, HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        searchServiceVo.setUser(uid);
        searchServiceVo.setStatus(Status.SERVICE_STATUS_ASSIGN);
        return new ResponseResult<>(ResponseResult.OK,serviceInvoiceService.queryServiceInvoiceById(searchServiceVo));
    }

    /**
     * 客户经理处理后业务
     * @param id
     * @return
     */
    @RequireMethod("客户经理处理后业务")
    @PutMapping ("/updateHandleStatus/{id}/{handleWay}")
    public ResponseResult<String>  updateHandleStatus(@PathVariable("id")String id,@PathVariable("handleWay")String handleWay){
        serviceInvoiceService.updateHandleStatus(id,handleWay);
        return new ResponseResult<>(ResponseResult.OK,null);
    }

    /**
     * 服务反馈界面根据客户经理id查询状态为已处理的服务单据
     * @param searchServiceVo
     * @return
     */
    @RequireMethod("根据客户经理id查询状态为已处理的服务单据")
    @PostMapping ("/queryServiceInvoiceByIdAndStatus")
    public ResponseResult<ResPage<List<ServiceInvoiceVo>>>  queryServiceInvoiceByIdAndStatus(@RequestBody SearchServiceVo searchServiceVo, HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        searchServiceVo.setUser(uid);
        searchServiceVo.setStatus(Status.SERVICE_STATUS_HANDLE);
        return new ResponseResult<>(ResponseResult.OK,serviceInvoiceService.queryServiceInvoiceByIdAndStatus(searchServiceVo));
    }

    /**
     * 根据反馈结果修改订单状态
     * @param id
     * @param mark
     * @return
     */
    @RequireMethod("根据反馈结果修改订单状态")
    @PutMapping("/updateMarks/{id}/{mark}/{content}")
    public ResponseResult<String>  updateMarks(@PathVariable("id")String id,@PathVariable("mark") Integer mark,@PathVariable("content")String content){
        serviceInvoiceService.updateMarks(id,mark,content);
        return new ResponseResult<>(ResponseResult.OK,null);
    }

    /**
     * 查询已归档的单据
     * @return
     */
//    @PostMapping("queryServiceInvoiceByStatus")
//    public ResponseResult<ResPage<List<ServiceInvoiceVo>>> queryServiceInvoiceByStatus(@RequestBody SearchServiceVo searchServiceVo){
//        return new ResponseResult<>(ResponseResult.OK,serviceInvoiceService.queryServiceInvoiceByStatus(searchServiceVo));
//    }
    @RequireMethod("查询已归档的单据")
    @GetMapping("queryServiceInvoiceByStatus/{pageNum}/{pageSize}")
    public ResponseResult<ResPage<List<ServiceInvoiceVo>>> queryServiceInvoiceByStatus(@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize")Integer pageSize){
        return new ResponseResult<>(ResponseResult.OK,serviceInvoiceService.queryServiceInvoiceByStatus(pageNum,pageSize));
    }

    /**
     * 修改单据状态为已归档
     * @param id
     * @return
     */
    @RequireMethod("修改单据状态为已归档")
    @PutMapping("/updateStatus/{id}")
    public ResponseResult<String>  updateStatus(@PathVariable("id")String id){
        serviceInvoiceService.updateStatus(id);
        return new ResponseResult<>(ResponseResult.OK,null);
    }
    /**
     * 客服服务分析根据类型统计
     */
    @RequireMethod("客服服务分析根据类型统计")
    @GetMapping("/ServiceTypeCount/{year}")
    public ResponseResult<List<ServiceTypeCountVo>> ServiceTypeCount(@PathVariable("year") String year){
        return new ResponseResult<>(ResponseResult.OK,serviceInvoiceService.ServiceTypeCount(year));
    }
}
